﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using omsData;

namespace myomscl.Utils
{
    public static class UserSecurity
    {
        public static bool Authenticate(string login, string password)
        {
            using (OmsModelContainer context = new OmsModelContainer())
            {
                var user = context.Users.Include("Organisation").Where(u => u.Login == login && u.Password == password).FirstOrDefault();
                if (user != null)
                {
                    if (SessionManager.Current == null)
                    {
                        SessionManager.Current = new WebSessionProvider();
                    }
                    SessionManager.Current[Constants.CurrentUser] = user;
                    return true;
                }
            }


            //UserDAL dal = new UserDAL();
            //var userList = dal.GetUser(login, password);
            //if (userList != null && userList.Rows.Count == 1)
            //{
            //    var userDO = userList[0];
            //    var user = new User()
            //    {
            //        Id = userDO.id,
            //        FirstName = userDO.firstname,
            //        LastName = userDO.lastname,
            //        Email = userDO.email,
            //        Notification = userDO.notification,
            //        Confirmation = userDO.confirmation,
            //        TypeId = userDO.typeid,
            //        Password = password,
            //        MunicipalityId = null,
            //        Name = userDO.organizationname
            //    };
                
            //    switch (userDO.type)
            //    {
            //        case "M":
            //            user.Type = WorkflowEnums.UserType.Municipality;
            //            user.MunicipalityId = userDO.typeid;
                        
            //            break;
            //        case "N":
            //            user.Type = WorkflowEnums.UserType.NetworkOperator;
                        
            //            break;
            //        case "C":
            //            user.Type = WorkflowEnums.UserType.Contractor;
                        
            //            break;
            //    }
                
            //    if (SessionManager.Current == null)
            //    {
            //        SessionManager.Current = new WebSessionProvider();
            //    }
            //    SessionManager.Current[Constants.CurrentUser] = user;
            //    return true;
            //}
            return false;
        }

        public static string OpenStartPage()
        {
            string url = string.Empty;
            User user = SessionManager.Current[Constants.CurrentUser] as User;

            if (!user.OrganisationReference.IsLoaded)
                user.OrganisationReference.Load();

            return string.Format("~/{0}/NewOrders.aspx", user.Organisation.Name);
            //switch (user.Type)
            //{
            //    case WorkflowEnums.UserType.Municipality:
            //        url = string.Format("~/{0}/Alerts/Newalerts.aspx", user.Name);
            //        break;
            //    case WorkflowEnums.UserType.NetworkOperator:
            //        url = string.Format("~/{0}/Alerts/Newalerts.aspx", "NetworkOperator");
            //        break;
            //    case WorkflowEnums.UserType.Contractor:
            //        break;
            //}
            //return url;
        }

        public static string ValidateEmail(string email)
        {
            using (OmsModelContainer context = new OmsModelContainer())
            {
                var user = context.Users.Where(u => u.Email == email).FirstOrDefault();
                if (user != null)
                    return user.Id.ToString();
            }
            
            //UserDAL dal = new UserDAL();
            //var userTable = dal.GetUserByEmail(email);
            //{
            //    if (userTable.Rows.Count == 1)
            //    {
            //        return userTable[0].id.ToString();
            //    }
            //}
            return null;
        }
    }
}
